Lesson 5 - Identify data structures and types

A lot of the time a developer will be creating software from some kind of problem definition or written scenario. The rough process to create code is to -

As programs get more complex then their design will start to focus around how to split the code into modules. This could be classes or functions. However that is a problem for another day!

To find out what needs to be stored you need to understand what is meant by the problem. The problem below is a bit of basic maths. Find the average, max and min of a set of test score. The question is what would need to be stored. First of all we clearly need to store the max, min and average once they have been calculated! We clearly need to store the test scores as well.

"Given a set of test scores you are to work out the average test score, the maximum test score and finally the lowest test code. "

Is that all we need to store? The above are the "obvious" input and output variables. Input variables (test scores) tend to be parameters of a function. Output variables tend to end up in print statements or as part of a return. What we need to find now are the hidden variables. Ones which are not part of the input or output but are used in order to calculate the output. To work out the average we need to add up all the values (sum) and divide by the number of values (length of test scores array). We need to store the total so we can perform the average calculation.

The other variable we will need during calculation is a counter for a loop. What loop i hear you ask! In order to calculate the values you will need to iterate over the test scores. A loop must have a variable to keep track of how far through the loop you are. So the final list of variables are -


def testScoreEval(testScores):
	min = testScores[0]
	max = testScores[0]
	total = 0
	for i in testScores:
		total = total + i
		if min < i: min = i
		if max > i: max = i
	
	average = total / len(testScores)
	print "max = ", max
	print "min = ", min
	print "average = ", average
				

The above code shows how the variables identified are used to solve the problem.